//
//  JFBDrawLineHelper.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/19.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBDrawLineHelper.h"

@interface JFBDrawLineHelper(){
    NSMutableArray  *_horizonLines;
    NSMutableArray *_verticalLines;

}

@end

@implementation JFBDrawLineHelper

- (instancetype)init {
    self = [super init];
    if( self ) {
        _horizonLines = [@[] mutableCopy];
        _verticalLines = [@[] mutableCopy];

        return self;
    }
    return nil;
}

-( void )addHorizontalLine:(NSUInteger)x y:(NSUInteger)y color:(UIColor*)color width:(NSUInteger)width{
    [_horizonLines addObject:[NSDictionary dictionaryWithObjects:@[[NSNumber numberWithInteger:x], [NSNumber numberWithInteger:y], color, [NSNumber numberWithInteger:width]] forKeys:@[@"x", @"y", @"color", @"width"]]];
}
-( void )addverticalLine:(NSUInteger)x y:(NSInteger)y color:(UIColor*)color height:(NSUInteger)height isDash:(BOOL)isDash{
    [_verticalLines addObject:[NSDictionary dictionaryWithObjects:@[[NSNumber numberWithInteger:x], [NSNumber numberWithInteger:y], color, [NSNumber numberWithInteger:height], (isDash ? [NSNumber numberWithInt:1]:[NSNumber numberWithInt:0])] forKeys:@[@"x", @"y", @"color", @"height", @"isDash"]]];
}


-(void)drawDashLineWithImageView :(UIImageView *)imageView1 withColor: (UIColor *)color
{
    imageView1.image = nil;
    UIGraphicsBeginImageContext(imageView1.frame.size);   //开始画线
    [imageView1.image drawInRect:CGRectMake(0, 0, imageView1.frame.size.width, imageView1.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //设置线条终点形状

    const CGFloat lengths[] = {3, 3};
    CGContextRef line = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(line, color.CGColor);
    
    CGContextSetLineWidth(line, 2.0);
    CGContextSetLineDash(line, 0, lengths, 1.0);  //画虚线
    CGContextMoveToPoint(line, 0.0, 0.0);    //开始画线
    CGContextAddLineToPoint(line, 0.0, imageView1.frame.size.height);
    CGContextStrokePath(line);
    
    imageView1.image = UIGraphicsGetImageFromCurrentImageContext();
}

- (void)drawRect:(CGContextRef)context{
    // 1.获取上下文环境
    
    CGContextSetLineWidth(context, 1.0);
    CGContextBeginPath(context);
    CGContextSetLineCap(context, kCGLineCapRound);
    
    for( NSDictionary *line in _verticalLines ) {
        // 2.设置线的颜色和宽度
        CGContextSetStrokeColorWithColor(context, ((UIColor*)[line objectForKey:@"color"]).CGColor);
        
        BOOL isDash = [(NSNumber*)[line objectForKey:@"isDash"] intValue] == 1;
        if( isDash ) {
            const CGFloat lengths[] = {3, 3};
            CGContextSetLineDash(context, 0, lengths, 1.0);
        } else {
            CGContextSetLineDash(context, 0, 0, 0);
        }
        
        int verticalX = [[line objectForKey:@"x"] intValue];
        int verticalBeginY = [[line objectForKey:@"y"] intValue];
        int verticalEndY = verticalBeginY + [[line objectForKey:@"height"]intValue];
        
        CGContextMoveToPoint(context, verticalX, verticalBeginY);
        CGContextAddLineToPoint(context, verticalX, verticalEndY);
        
        CGContextClosePath(context);
        CGContextStrokePath(context);
    }
    
    for( NSDictionary *line in _horizonLines ) {
        // 2.设置线的颜色和宽度
        CGContextSetStrokeColorWithColor(context, ((UIColor*)[line objectForKey:@"color"]).CGColor);
        
        CGContextSetLineDash(context, 0, 0, 0);
        
        int horizonBeginX = [[line objectForKey:@"x"] intValue];
        int horizonEndX = horizonBeginX + [[line objectForKey:@"width"] intValue];
        
        int horizonY = [[line objectForKey:@"y"] intValue];
        CGContextMoveToPoint(context, horizonBeginX, horizonY);
        CGContextAddLineToPoint(context, horizonEndX, horizonY);
        
        CGContextClosePath(context);
        CGContextStrokePath(context);
    }
}

@end
